package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.LegalPerson;
import net.hn.hnms.biz.project.mapper.LegalPersonMapper;
import net.hn.hnms.biz.project.request.LegalPersonReq;
import net.hn.hnms.biz.project.service.LegalPersonService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LegalPersonServiceImpl implements LegalPersonService {

    @Autowired
    private LegalPersonMapper legalPersonMapper;

    @Override
    public int insert(LegalPersonReq legalPersonReq) {
        LegalPerson insertObj = new LegalPerson();
        BeanUtil.copyProperties(legalPersonReq, insertObj);
        return legalPersonMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        legalPersonMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(LegalPersonReq legalPersonReq) {
        LegalPerson updateObj = new LegalPerson();
        BeanUtil.copyProperties(legalPersonReq, updateObj);
        int updateCount = legalPersonMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public LegalPerson selectDetailById(String id) {
        LegalPerson legalPerson = legalPersonMapper.selectById(id);
        if (legalPerson == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return legalPerson;
    }

    @Override
    public ResponsePage<LegalPerson> selectPageList(LegalPersonReq legalPersonReq, PageQuery pageQuery) {
        Page<LegalPerson> page = legalPersonMapper.selectPage(pageQuery.build(), buildQuery(legalPersonReq).orderByDesc(LegalPerson::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<LegalPerson> buildQuery(LegalPersonReq req){
        return new LambdaQueryWrapper<LegalPerson>()
                .eq(StrUtil.isNotBlank(req.getMineCode()), LegalPerson::getMineCode, req.getMineCode())
                .like(StrUtil.isNotBlank(req.getLegalPersonName()), LegalPerson::getLegalPersonName, req.getLegalPersonName())
                .like(StrUtil.isNotBlank(req.getCompany()), LegalPerson::getCompany, req.getCompany())
                .like(StrUtil.isNotBlank(req.getCompanyAddress()), LegalPerson::getCompanyAddress, req.getCompanyAddress());
    }
}
